package controller

import (
	"webstack/model"

	"github.com/gin-gonic/gin"
)

func AddWeb(c *gin.Context) {
	var m model.Web
	c.ShouldBind(&m)
	id, err := m.Add()
	if err != nil {
		c.JSON(200, gin.H{"id": 0, "errCode": 10, "errMsg": err.Error()})
	} else {
		c.JSON(200, gin.H{"id": id, "errCode": 0, "errMsg": ""})
	}
}

func DeleteWeb(c *gin.Context) {
	var m model.Web
	c.ShouldBind(&m)
	err := model.DeleteWeb(m.ID)
	if err != nil {
		c.JSON(200, gin.H{"errCode": 25, "errMsg": err.Error()})
	} else {
		c.JSON(200, gin.H{"errCode": 0, "errMsg": ""})
	}
}

func UpdateWeb(c *gin.Context) {
	var m model.Web
	c.ShouldBind(&m)
	err := m.Update()
	if err != nil {
		c.JSON(200, gin.H{"errCode": 36, "errMsg": err.Error()})
	} else {
		c.JSON(200, gin.H{"errCode": 0, "errMsg": ""})
	}
}

func GetUnCategoryWeb(c *gin.Context) {
	webs, err := model.GetUnCategoryWeb()
	if err != nil {
		c.JSON(200, gin.H{"webs": nil, "errCode": 25, "errMsg": err.Error()})
	} else {
		c.JSON(200, gin.H{"webs": webs, "errCode": 0, "errMsg": ""})
	}
}

type multiSetCategory struct {
	WebIDs []uint `json:"web_ids" binding:"required"`
	CateID uint   `json:"cate_id" binding:"required"`
}

func SetMultiCategory(c *gin.Context) {
	var m multiSetCategory
	c.ShouldBind(&m)
	err := model.SetCategoryByIds(m.WebIDs, m.CateID)
	if err != nil {
		c.JSON(200, gin.H{"errCode": 25, "errMsg": err.Error()})
	} else {
		c.JSON(200, gin.H{"errCode": 0, "errMsg": ""})
	}
}
